package com.guodu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.UnitsDao;
import com.guodu.dao.ViewMaterialChaoshiDao;
import com.guodu.entity.Units;
import com.guodu.entity.ViewMaterialCreate;
import com.guodu.entity.ViewMaterialShelves;
import com.guodu.service.ViewMaterialChaoshiService;
import com.guodu.vo.ChaoshiVo;
import com.guodu.vo.ViewMaterialChaoshiVo;

@Service("viewMaterialChaoshiService")
public class ViewMateiralChaoshiServiceImpl implements ViewMaterialChaoshiService {

	@Resource
	private ViewMaterialChaoshiDao viewMaterialChaoshiDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@SuppressWarnings("rawtypes")
	@Override
	public Map<String, ViewMaterialChaoshiVo> findByVo(ChaoshiVo chaoshiVo) {
		// TODO Auto-generated method stub
		List<ViewMaterialCreate> views1 = viewMaterialChaoshiDao.findCreateViewByVo(chaoshiVo);
		List<ViewMaterialShelves> views2= viewMaterialChaoshiDao.findShelvesViewByVo(chaoshiVo);
		Map<String,ViewMaterialChaoshiVo> map = new HashMap<String,ViewMaterialChaoshiVo>();
		for(ViewMaterialCreate view1 : views1){
			if(view1.getCreateTime().equals(views1.get(0).getCreateTime())){
				if(map.get(view1.getUnitsId().toString())==null){
					map.put(view1.getUnitsId().toString(), new ViewMaterialChaoshiVo(view1));
				}
			}
			if(view1.getCreateTime().equals(views1.get(views1.size()-1).getCreateTime())){
				if(map.get(view1.getUnitsId().toString())!=null){
					this.sub(map.get(view1.getUnitsId().toString()), view1);
				}
			}
		}
		for(ViewMaterialShelves view2:views2){
			if(map.get(view2.getUnitsId().toString())!=null){
				this.sum(map.get(view2.getUnitsId().toString()), view2);
			}else{
				map.put(view2.getUnitsId().toString(), new ViewMaterialChaoshiVo(view2));
			}
		}
		Map<String,ViewMaterialChaoshiVo> mapAll = new HashMap<String,ViewMaterialChaoshiVo>();
		mapAll.put("all", new ViewMaterialChaoshiVo());
		List<Units> funitses = unitsDao.findAllFunits();
		for(Units units : funitses){
			mapAll.put(units.getUnitsId().toString()+"all", new ViewMaterialChaoshiVo());
		}
		Set set = map.entrySet();
		Iterator iterator = set.iterator();
		while(iterator.hasNext()){
			Map.Entry mapEntity = (Entry) iterator.next();
			this.sumAll(mapAll.get("all"), (ViewMaterialChaoshiVo)mapEntity.getValue());
			this.sumAll(mapAll.get(((ViewMaterialChaoshiVo)mapEntity.getValue()).getFunitsId()+"all"), (ViewMaterialChaoshiVo)mapEntity.getValue());
		}
		map.putAll(mapAll);
		return map;
	}
	
	private void sub(ViewMaterialChaoshiVo view1, ViewMaterialCreate view2){
		view1.setLjMoney((view1.getLjMoney()!=null?view1.getLjMoney():new BigDecimal("0")).subtract(view2.getLjMoney()!=null?view2.getLjMoney():new BigDecimal("0")));
	}
	
	private void sum(ViewMaterialChaoshiVo view1, ViewMaterialShelves view2){
		view1.setShelvesMoney((view1.getShelvesMoney()!=null?view1.getShelvesMoney():new BigDecimal("0")).add(view2.getShevlesMoney()!=null?view2.getShevlesMoney():new BigDecimal("0")));
	}

	private void sumAll(ViewMaterialChaoshiVo view1, ViewMaterialChaoshiVo view2){
		view1.setShelvesMoney((view1.getShelvesMoney()!=null?view1.getShelvesMoney():new BigDecimal("0")).add(view2.getShelvesMoney()!=null?view2.getShelvesMoney():new BigDecimal("0")));
		view1.setLjMoney((view1.getLjMoney()!=null?view1.getLjMoney():new BigDecimal("0")).add(view2.getLjMoney()!=null?view2.getLjMoney():new BigDecimal("0")));
		view1.setMaterialCount((view1.getMaterialCount()!=null?view1.getMaterialCount():0L)+(view2.getMaterialCount()!=null?view2.getMaterialCount():0L));
		view1.setSupplierCount((view1.getSupplierCount()!=null?view1.getSupplierCount():0L)+(view2.getSupplierCount()!=null?view2.getSupplierCount():0L));
		view1.setMoney((view1.getMoney()!=null?view1.getMoney():new BigDecimal("0")).add(view2.getMoney()!=null?view2.getMoney():new BigDecimal("0")));
	}
}
